package helplib

import (
	"errors"
	"fmt"
	"math"
)

//BufferToString uint8切片转换成字符串。供显示
func BufferToString(buffer []uint8) string {
	output := ""
	for _, bb := range buffer {
		output += fmt.Sprintf(" 0x%x ", bb)
	}
	return output
}

func UShortsToInt16(data uint16) int16 {
	return int16(data)
}
func UShortsToUInt32Be(buffer []uint16) (uint32, error) {
	if buffer != nil && len(buffer) > 1 {
		return uint32(buffer[0])<<16 + uint32(buffer[1]), nil
	} else {
		return 0, errors.New("buffer is nil or empty")
	}
}
func UShortsToInt32Be(buffer []uint16) (int32, error) {
	if buffer != nil && len(buffer) > 1 {
		return int32(buffer[0])<<16 + int32(buffer[1]), nil
	} else {
		return 0, errors.New("buffer is nil or empty")
	}
}
func UShortsToUInt64Be(buffer []uint16) (uint64, error) {
	if buffer != nil && len(buffer) > 3 {
		return uint64(buffer[0])<<48 + uint64(buffer[1])<<32 + uint64(buffer[2])<<16 + uint64(buffer[3]), nil
	} else {
		return 0, errors.New("buffer is nil or empty")
	}
}
func UShortsToInt64Be(buffer []uint16) (int64, error) {
	if buffer != nil && len(buffer) > 1 {
		return int64(buffer[0])<<48 + int64(buffer[1])<<32 + int64(buffer[2])<<16 + int64(buffer[3]), nil
	} else {
		return 0, errors.New("buffer is nil or empty")
	}
}
func UShortsToUInt32Le(buffer []uint16) (uint32, error) {
	if buffer != nil && len(buffer) > 1 {
		return uint32(buffer[1])<<16 + uint32(buffer[0]), nil
	} else {
		return 0, errors.New("buffer is nil or empty")
	}
}
func UShortsToInt32Le(buffer []uint16) (int32, error) {
	if buffer != nil && len(buffer) > 1 {
		return int32(buffer[1])<<16 + int32(buffer[0]), nil
	} else {
		return 0, errors.New("buffer is nil or empty")
	}
}
func UShortsToUInt64Le(buffer []uint16) (uint64, error) {
	if buffer != nil && len(buffer) > 3 {
		return uint64(buffer[3])<<48 + uint64(buffer[2])<<32 + uint64(buffer[1])<<16 + uint64(buffer[0]), nil
	} else {
		return 0, errors.New("buffer is nil or empty")
	}
}
func UShortsToInt64Le(buffer []uint16) (int64, error) {
	if buffer != nil && len(buffer) > 1 {
		return int64(buffer[3])<<48 + int64(buffer[2])<<32 + int64(buffer[1])<<16 + int64(buffer[0]), nil
	} else {
		return 0, errors.New("buffer is nil or empty")
	}
}
func UShortsToReal32Be(buffer []uint16) (float32, error) {
	if res, err := UShortsToUInt32Be(buffer); err == nil {
		return math.Float32frombits(res), nil
	} else {
		return 0, err
	}
}

func UShortsToReal32Le(buffer []uint16) (float32, error) {
	if res, err := UShortsToUInt32Le(buffer); err == nil {
		return math.Float32frombits(res), nil
	} else {
		return 0, err
	}
}

func UShortsToReal64Be(buffer []uint16) (float64, error) {
	if res, err := UShortsToUInt64Be(buffer); err == nil {
		return math.Float64frombits(res), nil
	} else {
		return 0, err
	}
}

func UShortsToReal64Le(buffer []uint16) (float64, error) {
	if res, err := UShortsToUInt64Le(buffer); err == nil {
		return math.Float64frombits(res), nil
	} else {
		return 0, err
	}
}

func Float64ToUShortByU16(value float64) (uint16, error) {
	if value > 65535 || value < 0 {
		return 0, errors.New("数据超出范围")
	} else {
		return uint16(value), nil
	}
}

func Float64ToUShortByS16(value float64) (uint16, error) {
	if value > 32767 || value < -32768 {
		return 0, errors.New("数据超出范围")
	} else {
		return uint16(int16(value)), nil
	}
}

func Float64ToUShortsLeByF32(value float64) ([]uint16, error) {
	if value > 340282346638528859811704183484516925440 || value < -340282346638528859811704183484516925440 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := math.Float32bits(float32(value))
		return []uint16{uint16(temp & 0xffff), uint16(temp >> 16)}, nil
	}
}

func Float64ToUShortsBeByF32(value float64) ([]uint16, error) {
	if value > 340282346638528859811704183484516925440 || value < -340282346638528859811704183484516925440 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := math.Float32bits(float32(value))
		return []uint16{uint16(temp >> 16), uint16(temp & 0xffff)}, nil
	}
}

func Float64ToUShortsLe(value float64) ([]uint16, error) {
	temp := math.Float64bits(value)
	return []uint16{uint16(temp & 0xffff), uint16(temp >> 16), uint16(temp >> 32), uint16(temp >> 48)}, nil
}

func Float64ToUShortsBe(value float64) ([]uint16, error) {
	temp := math.Float64bits(value)
	return []uint16{uint16(temp >> 48), uint16(temp >> 32), uint16(temp >> 16), uint16(temp & 0xffff)}, nil
}

func Float64ToUShortsBeByS32(value float64) ([]uint16, error) {
	//-2,147,483,648 到 2,147,483,647
	if value > 2147483647 || value < -2147483648 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := uint32(value)
		return []uint16{uint16((temp >> 16) & 0xffff), uint16(temp & 0xffff)}, nil
	}
}

func Float64ToUShortsLeByS32(value float64) ([]uint16, error) {
	if value > 2147483647 || value < -2147483648 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := uint32(value)
		return []uint16{uint16(temp & 0xffff), uint16((temp >> 16) & 0xffff)}, nil
	}
}

func Float64ToUShortsBeByU32(value float64) ([]uint16, error) {
	if value > 4294967295 || value < 0 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := uint32(value)
		return []uint16{uint16((temp >> 16) & 0xffff), uint16(temp & 0xffff)}, nil
	}
}

func Float64ToUShortsLeByU32(value float64) ([]uint16, error) {
	if value > 4294967295 || value < 0 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := uint32(value)
		return []uint16{uint16(temp & 0xffff), uint16((temp >> 16) & 0xffff)}, nil
	}
}

func Float64ToUShortsBeByS64(value float64) ([]uint16, error) {
	if value > 9223372036854775807 || value < -9223372036854775808 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := uint64(value)
		return []uint16{uint16((temp >> 48) & 0xffff), uint16((temp >> 32) & 0xffff), uint16((temp >> 16) & 0xffff), uint16(temp & 0xffff)}, nil
	}
}

func Float64ToUShortsLeByS64(value float64) ([]uint16, error) {
	if value > 9223372036854775807 || value < -9223372036854775808 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := uint64(value)
		return []uint16{uint16(temp & 0xffff), uint16((temp >> 16) & 0xffff), uint16((temp >> 32) & 0xffff), uint16((temp >> 48) & 0xffff)}, nil
	}
}

func Float64ToUShortsBeByU64(value float64) ([]uint16, error) {
	if value > 18446744073709551615 || value < 0 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := uint64(value)
		return []uint16{uint16((temp >> 48) & 0xffff), uint16((temp >> 32) & 0xffff), uint16((temp >> 16) & 0xffff), uint16(temp & 0xffff)}, nil
	}
}

func Float64ToUShortsLeByU64(value float64) ([]uint16, error) {
	if value > 18446744073709551615 || value < 0 {
		return nil, errors.New("数据超出范围")
	} else {
		temp := uint64(value)
		return []uint16{uint16(temp & 0xffff), uint16((temp >> 16) & 0xffff), uint16((temp >> 32) & 0xffff), uint16((temp >> 48) & 0xffff)}, nil
	}
}
